package com.firexxx.study.designmodel.sixconcept.requirement.addprocessstep;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 利用代理模式来保持开闭
 * @author: zhuangzhang2
 * @date: 2021-01-04 16:10
 */
public class MaxPreferentialService implements IPreferential {

    private List<IPreferential> preferentials = new ArrayList<>();

    public MaxPreferentialService() {
        //想要隔离对象创建的变化，可以封装一个简单工厂类
        preferentials.add(new DiscountService());
        preferentials.add(new ReduceService());
    }

    @Override
    public int preferential(int price) {
        int min = 0;

        for (IPreferential preferential : preferentials) {
            int result = preferential.preferential(price);
            if (min == 0) {
                min = result;
            } else if (min > result) {
                min = result;
            }
        }

        return min;
    }

}
